import pandas as pd
from sklearn.model_selection import validation_curve
from sklearn.ensemble import GradientBoostingRegressor as GBDT
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

boston = pd.read_csv('boston.csv')
boston_data = boston.values[:,:13]
boston_target = boston.values[:,13:14].ravel()

def train_and_test():
    x_train, x_test, y_train, y_test = train_test_split(boston_data, boston_target)
    model = GBDT(n_estimators=140)
    model.fit(x_train, y_train)

    train_score = model.score(x_train, y_train)
    test_score = model.score(x_test, y_test)

    print(f"train score:{train_score:.2f}")
    print(f" test score:{test_score:.2f}")

def ke_shi_hua():
    param_range = range(20, 150, 5)
    train_score, val_score = validation_curve(
        GBDT(max_depth=3), boston_data, boston_target,
        param_name='n_estimators',
        param_range=param_range,
        cv=5,
        n_jobs=-1  # 使用所有CPU核心进行并行计算，加速过程
    )
    train_mean = train_score.mean(axis=-1)
    train_std = train_score.std(axis=-1)
    val_mean = val_score.mean(axis=-1)
    val_std = val_score.std(axis=-1)

    fig, axs = plt.subplots(1, 2, figsize=(12, 6))
    axs[0].plot(param_range, train_mean)
    axs[0].fill_between(param_range, train_mean - train_std, train_mean + train_std, alpha=0.2)
    axs[0].set_title('train score')
    axs[0].set_xlabel("n_estimators")
    axs[0].set_ylabel('score')

    axs[1].plot(param_range, val_mean, label='Validation score')
    axs[1].fill_between(param_range, val_mean - val_std, val_mean + val_std, alpha=0.2)
    axs[1].set_title('validation score')
    axs[1].set_xlabel("n_estimators")
    axs[1].set_ylabel('score')
    plt.show()

if __name__ == '__main__':
    train_and_test()
    ke_shi_hua()
